package com.yiqi.zhiyuan.feature.login.presenter;

import android.net.Uri;
import android.text.TextUtils;

import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.ValidatorUtil;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.romgim.SealAppContext;
import com.yiqi.zhiyuan.entity.dto.RegisterDTO;
import com.yiqi.zhiyuan.entity.enums.VCodeType;
import com.yiqi.zhiyuan.feature.login.contract.LoginContract;
import com.yiqi.zhiyuan.feature.login.dependencies.login.DaggerLoginPresenterComponent;
import com.yiqi.zhiyuan.framework.ZYConstants;

import javax.inject.Inject;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.UserInfo;

/**
 * @ProjectName: ZhiYuan
 * @Description: java类作用描述
 * @Author: lijuwen
 * @CreateDate: 2019/3/14 10:54
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/14 10:54
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class LoginPresenter implements LoginContract.Presenter {

    private LoginContract.View mView;

    @Inject
    LoginContract.Model mModel;


    private Disposable mDisposable;

    public LoginPresenter(LoginContract.View mView) {
        this.mView = mView;
        DaggerLoginPresenterComponent.create().inject(this);
    }

    @Override
    public void sendCode(String mobile) {
        if (TextUtils.isEmpty(mobile) || !ValidatorUtil.isMobile(mobile)) {
            mView.showGetCodeError(AM.res().getString(R.string.toast_phone_error));
            return;
        }

        mModel.doSendCode(mobile, VCodeType.LOGIN.type())
                .compose(mView.<String>bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoading();
                        mView.showGetCodeSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showGetCodeError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void loginByPwd(String mobile, String password, Integer loginType) {
        if (TextUtils.isEmpty(mobile) || !ValidatorUtil.isMobile(mobile)) {
            mView.showLoginError(AM.res().getString(R.string.toast_phone_error));
            return;
        }
        if (TextUtils.isEmpty(password)) {
            mView.showLoginError(AM.res().getString(R.string.toast_password_input));
            return;
        }
        if (password.length() < 6) {
            mView.showLoginError(AM.res().getString(R.string.toast_password_6));
            return;
        }

        RegisterDTO registerDTO = new RegisterDTO();
        registerDTO.setMobile(mobile);
        registerDTO.setPassword(password);
        registerDTO.setLoginType(loginType);
        registerDTO.setRegistrationId(JPushInterface.getRegistrationID(AM.app()));

        Logger.d("--》" + registerDTO.toString());

        mModel.doLogin(registerDTO)
                .compose(mView.<UserInfoBo>bindToLife())
                .subscribe(new Observer<UserInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(UserInfoBo s) {
                        mView.dismissLoading();
                        loginSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showLoginError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void loginByCode(String mobile, String vcode, Integer loginType) {
        if (TextUtils.isEmpty(mobile) || !ValidatorUtil.isMobile(mobile)) {
            mView.showLoginError(AM.res().getString(R.string.toast_phone_error));
            return;
        }
        if (TextUtils.isEmpty(vcode)) {
            mView.showLoginError(AM.res().getString(R.string.toast_code_input));
            return;
        }

        RegisterDTO registerDTO = new RegisterDTO();
        registerDTO.setMobile(mobile);
        registerDTO.setVcode(vcode);
        registerDTO.setLoginType(loginType);
        registerDTO.setRegistrationId(JPushInterface.getRegistrationID(AM.app()));


        mModel.doLogin(registerDTO)
                .compose(mView.<UserInfoBo>bindToLife())
                .subscribe(new Observer<UserInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(UserInfoBo s) {
                        mView.dismissLoading();
                        loginSuccess(s);
                    }

                    @Override
                    public void onError(Throwable e) {

                        mView.dismissLoading();
                        mView.showLoginError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void logout() {
        mModel.doLogout()
                .compose(mView.<UserInfoBo>bindToLife())
                .subscribe(new Observer<UserInfoBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(UserInfoBo s) {
                        mView.dismissLoading();
//                        loginSuccess(s);
                        mView.showLoginSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showLoginError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void start() {

    }

    private void loginSuccess(UserInfoBo value) {
        Logger.d("登录成功 ==== " + value.toString());
        AM.user().saveLoginResultCache(value == null ? null : value);
        RongIM.getInstance().setCurrentUserInfo(new UserInfo(value.getId(), value.getUsername(),
                TextUtils.isEmpty(value.getAvatarUrl()) ? Uri.parse("") : Uri.parse(value.getAvatarUrl())));
        RongIM.getInstance().setMessageAttachedUserInfo(true);
        RongIM.connect(ZYConstants.RONG_TOKEN, SealAppContext.getInstance().getConnectCallback());
        mView.showLoginSuccess();
    }
}
